<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="5">
					<a-form-item label="公司名称" name="companyName">
						<a-input v-model:value="searchFormState.companyName" placeholder="请输入公司名称" />
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="公司类型" name="companyType">
						<a-select
							v-model:value="searchFormState.companyType"
							:options="companyTypeOptions"
							allow-clear
							placeholder="请选择公司类型"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="4" v-show="advanced">
					<a-form-item label="联系电话" name="companyTel">
						<a-input v-model:value="searchFormState.companyTel" placeholder="请输入联系电话" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="服务期限" name="companyLimited">
						<a-range-picker v-model:value="searchFormState.companyLimited"  value-format="YYYY-MM-DD" format="YYYY-MM-DD"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'" />
					</a>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('homeSettingCompanyAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('homeSettingCompanyBatchDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchHomeCompany"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">

				<template v-if="column.dataIndex === 'companyType'">
					<a-tag :bordered="false" color="success" v-if="record.companyType === '0'">
						{{ '供应链平台' }}
					</a-tag>
					<a-tag :bordered="false" color="processing" v-if="record.companyType === '1'">
						{{ '供应商' }}
					</a-tag>
					<a-tag :bordered="false" color="warning" v-if="record.companyType === '2'">
						{{ '采购商' }}
					</a-tag>
				</template>
				<template v-if="column.dataIndex === 'companyLimited'">
					{{ record.command !== null ? record.companyLimited.substring(0, 10) : '' }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('homeSettingCompanyEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['homeSettingCompanyEdit', 'homeSettingCompanyDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteHomeCompany(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('homeSettingCompanyDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="homeSettingCompany">
import { cloneDeep } from 'lodash-es'
import Form from './form.vue'
import homeCompanyApi from '@/api/home/setting/homeCompanyApi'
const searchFormState = ref({})
const searchFormRef = ref()
const tableRef = ref()
const formRef = ref()
const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
// 公司类型字典
let companyTypeOptions = [{label:'供应链平台',value:'0'},{label:'供应商',value:'1'},{label:'采购商',value:'2'}]
// 查询区域显示更多控制
const advanced = ref(false)
const toggleAdvanced = () => {
	advanced.value = !advanced.value
}
const columns = [
	{
		title: '公司名称',
		dataIndex: 'companyName'
	},
	{
		title: '简称',
		dataIndex: 'companyAbbreviation',
		checked:false
	},
	{
		title: '公司类型',
		dataIndex: 'companyType'
	},
	{
		title: '地址',
		dataIndex: 'companyAddress',
		ellipsis: true
	},
	{
		title: '联系电话',
		dataIndex: 'companyTel'
	},
	{
		title: '联系人',
		dataIndex: 'companyContact'
	},
	{
		title: '公众号APPID',
		dataIndex: 'companyAppid',
		checked: false
	},
	{
		title: '公众号APPSECRET',
		dataIndex: 'companyAppsecret',
		checked: false
	},
	{
		title: '剩余短信(条)',
		dataIndex: 'companySmsRemain',
		checked: false
	},
	{
		title: '服务期限',
		dataIndex: 'companyLimited'
	},
	{
		title: '备注',
		dataIndex: 'remark',
		checked: false
	}
]
// 操作栏通过权限判断是否显示
if (hasPerm(['homeSettingCompanyEdit', 'homeSettingCompanyDelete'])) {
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: 150
	})
}
const selectedRowKeys = ref([])
// 列表选择配置
const options = {
	// columns数字类型字段加入 needTotal: true 可以勾选自动算账
	alert: {
		show: false,
		clear: () => {
			selectedRowKeys.value = ref([])
		}
	},
	rowSelection: {
		onChange: (selectedRowKey, selectedRows) => {
			selectedRowKeys.value = selectedRowKey
		}
	}
}
const loadData = (parameter) => {
	const searchFormParam = cloneDeep(searchFormState.value)
	// companyLimited范围查询条件重载
	if (searchFormParam.companyLimited) {
		searchFormParam.startCompanyLimited = searchFormParam.companyLimited[0]
		searchFormParam.endCompanyLimited = searchFormParam.companyLimited[1]
		delete searchFormParam.companyLimited
	}
	return homeCompanyApi.homeCompanyPage(Object.assign(parameter, searchFormParam)).then((data) => {
		return data
	})
}
// 重置
const reset = () => {
	searchFormRef.value.resetFields()
	tableRef.value.refresh(true)
}
// 删除
const deleteHomeCompany = (record) => {
	let params = [
		{
			id: record.id
		}
	]
	homeCompanyApi.homeCompanyDelete(params).then(() => {
		tableRef.value.refresh(true)
	})
}
// 批量删除
const deleteBatchHomeCompany = (params) => {
	homeCompanyApi.homeCompanyDelete(params).then(() => {
		tableRef.value.clearRefreshSelected()
	})
}
</script>
